package com.xpgk.mvc.statistic.monthlySaleTotalStatistic;

import java.util.Date;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.xpgk.mvc.base.BaseController;
import com.xpgk.mvc.codeSet.Code;
import com.xpgk.mvc.organization.Organization;
import com.xpgk.mvc.productPrice.Price;
import com.xpgk.mvc.productPrice.ProductPrice;
import com.xpgk.mvc.statistic.dailyTotalStatistic.DailyNursingTotal;
import com.xpgk.tools.ToolDateTime;

public class MonthlySaleTotalStatisticController  extends  BaseController{
	
	//private  DailySaleTotalStatisticServices   dailyTotalStatisticServices;
	
	public  void  index()
	{	
		String organids = getCUserOrganIds();
		Organization  organ = Organization.dao.findById(organids);
		String year = ToolDateTime.format(new Date(), ToolDateTime.pattern_y);
		setAttr("organization", organ);
		setAttr("year", year);
		render("monthlyTotalSaleStatistic.html");
	}
	@Before({Tx.class })
	public  void  getMonthlyTotal()
	{
		String year = getPara("year");
//		String month = getPara("month");
		String organids = getPara("organIds");
//		String sql  = "SELECT *  from  sb_customer_total_daily  WHERE  organids = ?  AND  date LIKE ? ORDER BY date";
		String sql  = "SELECT MONTH (date) as date, "+
						"sum(cjrs) AS xzrs, "+
						"sum(zjscjrs) AS zjscjrs, "+
						"sum(zrjdcjrs) AS zrjdcjrs, "+
						"sum(zr) AS zr, "+
						"sum(zc) AS zc, "+
						"sum(priceTotal) AS priceTotal, "+
						"sum(lc002salnum) AS lc002salnum, "+
						"sum(lc002salprice) AS lc002salprice, "+
						"sum(lc001salnum) AS lc001salnum, "+
						"sum(lc001salprice) AS lc001salprice, "+
						"sum(hf001salnum) AS hf001salnum, "+
						"sum(hf001salprice) AS hf001salprice, "+
						"sum(hf002salnum) AS hf002salnum, "+
						"sum(hf002salprice) AS hf002salprice, "+
						"sum(hf003salnum) AS hf003salnum, "+
						"sum(hf003salprice) AS hf003salprice, "+
						"sum(hf004salnum) AS hf004salnum, "+
						"sum(hf004salprice) AS hf004salprice, "+
						"sum(hf005salnum) AS hf005salnum, "+
						"sum(hf005salprice) AS hf005salprice, "+
						"sum(hf011salnum) AS hf011salnum, "+
						"sum(hf011salprice) AS hf011salprice, "+
						"sum(ss001salnum) AS ss001salnum, "+
						"sum(ss001salprice) AS ss001salprice, "+
						"sum(ss002salnum) AS ss002salnum, "+
						"sum(ss002salprice) AS ss002salprice "+
					"FROM "+
						"sb_customer_total_daily "+
					"WHERE "+
						"organids = ? "+
					"AND DATE_FORMAT(date, '%Y') = ? "+
					"GROUP BY "+
						"MONTH (date) ";
//		List<DailyNursingTotal>  dntList =  DailyNursingTotal.dao.find(sql,organids,"%"+month+"%");
		List<DailyNursingTotal>  dntList =  DailyNursingTotal.dao.find(sql,organids,year);
		double dyxslj = 0d;
		Organization organization = Organization.dao.findById(organids);
		Price price = Price.dao.findFirst("SELECT * FROM sb_shop_price WHERE organizationIds = ? ",organization.getStr(Organization.column_parentIds));
		String sql2 = "SELECT * FROM sb_product_price WHERE priceIds = ? AND productIds = ? ";
		double dlcdj = 0d;
		double bzlcdj = 0d;
		if(price!=null){
			ProductPrice dlcPrice = ProductPrice.dao.findFirst(sql2,price.getStr(Price.column_ids),Code.dlc_ids);
			ProductPrice bzlcPrice = ProductPrice.dao.findFirst(sql2,price.getStr(Price.column_ids),Code.bzlc_ids);
			dlcdj =  (double)dlcPrice.getFloat(ProductPrice.column_outPrice);
			bzlcdj = (double) bzlcPrice.getFloat(ProductPrice.column_outPrice);
		}
		
		for(int i = 0;i<dntList.size();i++){
			
			int month = dntList.get(i).getInt("date");
			String monthStr = month+"";
			if(month<10){
				monthStr = "0"+monthStr;
			}
			String yearMonth = year+"-"+monthStr;//2017-04
			dntList.get(i).set("date", yearMonth);
			dyxslj = dyxslj+dntList.get(i).getDouble(DailyNursingTotal.column_priceTotal);
			dntList.get(i).set("dyxslj", dyxslj);
			dntList.get(i).set("rjdc",dlcdj);
			dntList.get(i).set("jzzxse", bzlcdj);
		}
		
		
		//添加旧数据到日综合查询表
//		dailyTotalStatisticServices.saveOld();
		renderJson(dntList);
	}
	
	
	/**
	 * 查询门店销售日报表（弹窗）
	 * */
	public  void queryDaily()
	{	
		String  organids=getPara("organids");
		Organization org = Organization.dao.findById(organids);
		setAttr("organization", org);	
		render("/dlstasticinfo/dailySaleTotal.html");
	}

}
